import * as fs from "node:fs";
import * as path from "node:path";
import { Image, Pixel } from "../hp3-img.js";
import { loadPng, savePng } from "../hp3-png.js";

/**
 * 使用区域插值算法缩小图像
 * @param src 源图像
 * @param newWidth 新的宽度
 * @param newHeight 新的高度
 * @returns 缩小后的图像
 */
export function resizeImageAreaInterpolation(src: Image, newWidth: number, newHeight: number, alphaMapping?: number[][]): Image {

    const dst: Image = {
        width: newWidth,
        height: newHeight,
        pixels: new Array<Pixel>(newWidth * newHeight)
    };

    // 计算缩放比例
    const scaleX = src.width / newWidth;
    const scaleY = src.height / newHeight;

    // 对目标图像的每个像素进行区域插值
    for (let y = 0; y < newHeight; y++) {
        for (let x = 0; x < newWidth; x++) {
            // 计算源图像中对应的区域
            const srcX0 = Math.floor(x * scaleX);
            const srcY0 = Math.floor(y * scaleY);
            const srcX1 = Math.min(Math.ceil((x + 1) * scaleX), src.width);
            const srcY1 = Math.min(Math.ceil((y + 1) * scaleY), src.height);

            // 初始化累加器
            let totalR = 0, totalG = 0, totalB = 0, totalA = 0;
            let pixelCount = 0;

            // 计算区域内所有像素的平均值
            for (let sy = srcY0; sy < srcY1; sy++) {
                for (let sx = srcX0; sx < srcX1; sx++) {
                    const srcPixel = src.pixels[sy * src.width + sx];
                    totalR += srcPixel.r;
                    totalG += srcPixel.g;
                    totalB += srcPixel.b;
                    totalA += srcPixel.a;
                    pixelCount++;
                }
            }

            // 计算平均颜色值
            const dstPixel = {
                r: 0,
                g: 0,
                b: 0,
                a: 0
            };
            if (pixelCount > 0) {
                dstPixel.r = Math.round(totalR / pixelCount);
                dstPixel.g = Math.round(totalG / pixelCount);
                dstPixel.b = Math.round(totalB / pixelCount);
                dstPixel.a = Math.round(totalA / pixelCount);
                if (alphaMapping) {
                    let found = false;
                    for(const [l,v] of alphaMapping){
                        if(dstPixel.a<l){
                            dstPixel.a = v;
                            found = true;
                            break;
                        }
                    }
                    if(!found){
                        dstPixel.a = 255;
                    }
                }
            }
            dst.pixels[y * newWidth + x] = dstPixel;
        }
    }

    return dst;
}

// const inputDir = "E:\\gamedev\\resources\\拼格子塔防\\角色\\序列帧\\ok-large";
// const outputDir = "E:\\gamedev\\resources\\拼格子塔防\\角色\\序列帧\\ok-auto";
const inputDir = "E:\\gamedev\\resources\\拼格子塔防\\子弹\\改图";
const outputDir = "E:\\gamedev\\resources\\拼格子塔防\\子弹\\ok-auto";
const pattern = /bullet-.+\.png/;

const newWidth = 32;
const newHeight = 11;

const MAPPING_BINARY = [[128,0]];

// 确保输出目录存在
if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
}

// 获取输入目录中的所有文件
const files = fs.readdirSync(inputDir);

// 处理每个PNG文件
for (const file of files) {
    if(file.match(pattern)){
        console.error("开始处理："+file);
        const inputFile = path.join(inputDir, file);
        const outputFile = path.join(outputDir, file);
        const src = loadPng(inputFile);
        const dst = resizeImageAreaInterpolation(src,newWidth,newHeight);
        savePng(dst.width,dst.height,dst.pixels,outputFile);
        console.error("处理完成："+file);
    }
}
